home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT68 / WGT68A.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  4.6 KB  |  203 lines

  1. #include <wgt5.h>
  2.  
  3. /*
  4. ==============================================================================
  5.               WordUp Graphics Toolkit Version 5.0
  6.                  Demonstration Program 68a
  7.  
  8.  This example shows how to make a scrolling image with depth.  It scrolls
  9.  each horizontal line of the ground by different amounts, the fastest
  10.  being closest to you.
  11.  
  12.  *** PROJECT ***
  13.  This program requires the WGT5_WC.LIB file to be linked.
  14.  
  15.  *** DATA FILES ***
  16.  STREET.PAL, RUN.SPR
  17.                                WATCOM C++ VERSION
  18. ==============================================================================
  19. */
  20.  
  21. color pal[256];         /* The palette used for every graphic image */
  22. block sprites[200];     /* Array of images for the running robot */
  23.  
  24. block background;       /* Holds the background scrolling image */
  25. block work;             /* Page for constructing each frame */
  26.  
  27. int oldmode;
  28.  
  29. /* A structure which holds the scrolling values for each horizontal line */
  30. typedef struct
  31.  {
  32.   int x;                /* Current x value, shifted by 8 */
  33.   int increment;        /* fixed point increment */
  34.  } line_scroll;
  35. line_scroll lines[80];  /* 80 scrolling lines along the ground */
  36.  
  37. /* A simple sprite structure */
  38. typedef struct
  39.  {
  40.   int x;
  41.   int y;
  42.   int anm;              /* Sprite number */
  43.  } sprite;
  44. sprite people[5];
  45.  
  46.  
  47. int backx = 0;          /* X value for the scrolling rocks */
  48. int backinc;            /* X increment for scrolling rocks */
  49.  
  50.  
  51. /* Loads the graphics files, and allocates buffers */
  52. void load_graphics (void)
  53. {
  54.  work = wallocblock (320, 200);
  55.  /* Allocate a work buffer */
  56.  
  57.  wloadsprites (pal, "run.spr", sprites, 0, 199);
  58.  background = wloadpak ("street.pak");
  59.  wsetpalette (0, 255, pal);
  60. }
  61.  
  62.  
  63.  
  64. /* Frees the buffers and sprites */
  65. void free_graphics (void)
  66. {
  67.  wfreesprites (sprites, 0, 199);
  68.  wfreeblock (background);
  69.  wfreeblock (work);
  70. }
  71.  
  72.  
  73. /* Set up the initial scrolling values */
  74. void init_lines (void)
  75. {
  76. int i;
  77. int inc;
  78.  
  79.  inc = 128;     /* slowest scrolling speed (128/256 of a pixel */
  80.  
  81.  backx = 0;     /* rocks x value */
  82.  backinc = inc; /* rocks same speed as the ground */
  83.  
  84.  for (i = 0; i < 80; i++)
  85.   {
  86.    lines[i].x = 0;              /* clear out the x value */
  87.    lines[i].increment = inc;    /* set the scroll speed */
  88.    inc += 32;                   /* Make the next row move faster */
  89.   }
  90.  
  91. }
  92.  
  93.  
  94.  
  95. /* Construct the background image */
  96. void animate_lines (void)
  97. {
  98. block source1, source2;
  99. block dest1, dest2;
  100. block origsource, origdest;
  101. int i;
  102. int x;
  103.  
  104.  wcopyscreen (0, 0, 319, 51, background, 0, 0, work);
  105.  /* Draw the moon stationary */
  106.  
  107.  
  108.  x = backx >> 8;
  109.  
  110.  /* Copy the rocks */
  111.  wcopyscreen (x, 52, 319, 119, background, 0, 52, work);
  112.  if (x > 0)
  113.    wcopyscreen (0, 52, x-1, 119, background, 320 - x, 52, work);
  114.  
  115.  /* Scroll the rocks */
  116.  backx += backinc;
  117.  if (backx >= 81920)   /* 81920 is 320 << 8 */
  118.      backx -= 81920;
  119.  
  120.  
  121.  origdest = abuf + 120 * 320;           /* First row to copy */
  122.  origsource = background + 120 * 320;   /* First row to copy */
  123.  
  124.  for (i = 0; i < 80; i++)
  125.   {
  126.    /* Scroll this line */
  127.    lines[i].x += lines[i].increment;
  128.    if (lines[i].x >= 81920)   /* 81920 is 320 << 8, wraps scroll around */
  129.       lines[i].x -= 81920;
  130.  
  131.  
  132.    x = lines[i].x >> 8;
  133.    /* Get the x coord */
  134.  
  135.    dest1 = origdest + i * 320;
  136.    dest2 = dest1 + (319 - x);
  137.    source1 = origsource + i * 320;
  138.    source2 = source1 + x;
  139.    
  140.    /* Copy the line in two steps */
  141.    memcpy (dest1, source2, 320 - x);
  142.    if (x > 0)
  143.     memcpy (dest2, source1, x + 1);
  144.    
  145.   }
  146.  
  147. }
  148.  
  149.  
  150.  
  151. /* Animates and displays the running man */
  152. void animate_man (void)
  153. {
  154.  people[0].anm++;
  155.  if (people[0].anm > 29)
  156.    people[0].anm = 0;
  157.  
  158.  wputblock (people[0].x, people[0].y, sprites[people[0].anm], 1);
  159. }
  160.  
  161.  
  162.  
  163. void main (void)
  164. {
  165.   oldmode = wgetmode ();
  166.   if (!vgadetected ())
  167.   {
  168.     printf ("VGA is required to run this program...");
  169.     exit (1);
  170.   }
  171.  
  172.   printf ("WGT Example #68a\n\n");
  173.   printf ("This example shows how to make a scrolling image with depth.  It scrolls\n");
  174.   printf ("each horizontal line of the ground by different amounts, the fastest\n");
  175.   printf ("being closest to you.\n");
  176.   printf ("\nPress any key to begin.\n");
  177.   getch ();
  178.  
  179.   vga256 ();
  180.  
  181.   load_graphics ();
  182.  
  183.   init_lines ();
  184.  
  185.   /* Set the position of the running man */
  186.   people[0].x = 120;
  187.   people[0].y = 50;
  188.   people[0].anm = 0;
  189.  
  190.  
  191.   do {
  192.    wsetscreen (work);
  193.    animate_lines ();
  194.    animate_man ();
  195.    wnormscreen ();
  196.    wretrace ();
  197.    wputblock (0, 0, work, 0);
  198.    } while (!kbhit ());
  199.  
  200.   free_graphics ();
  201.   wsetmode (oldmode);
  202. }
  203.